package com.me.springcloud.hystrix;

import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixObservableCommand;

import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;

public class CommandHelloWorld extends HystrixObservableCommand<String> {

	private final String name;

	

	public CommandHelloWorld(String name) {
		super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
		this.name = name;
	}

	@Override
	protected Observable<String> construct() {
		throw new RuntimeException("aa");
//		return Observable.create(new Observable.OnSubscribe<String>() {
//			@Override
//			public void call(Subscriber<? super String> observer) {
//				try {
//					if (!observer.isUnsubscribed()) {
//						// a real example would do work like a network call here
//						observer.onNext("Hello");
//						observer.onNext(name + "!");
//						observer.onCompleted();
//					}
//				} catch (Exception e) {
//					observer.onError(e);
//				}
//			}
//		}).subscribeOn(Schedulers.io());
	}

	@Override
	protected Observable<String> resumeWithFallback() {
		return Observable.create(new Observable.OnSubscribe<String>() {
			@Override
			public void call(Subscriber<? super String> observer) {
				try {
					if (!observer.isUnsubscribed()) {
						// a real example would do work like a network call here
						observer.onNext(name + "11111111111!");
						observer.onCompleted();
					}
				} catch (Exception e) {
					observer.onError(e);
				}
			}
		}).subscribeOn(Schedulers.io());
	}

}